﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Data.SqlClient;
using SMS.WinForms;

namespace SMS.Console.Controls
{
    public partial class DatabaseConfigPage : ConfigPage
    {
        XmlNode connNode;
        SqlConnectionStringBuilder connBuilder = null;
        public DatabaseConfigPage()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            XmlDocument xDoc = base.Document;

            connNode = xDoc.SelectSingleNode("configuration/appSettings/add");
            if (connNode != null)
            {
                XmlAttribute xAttr = connNode.Attributes["value"];
                string connStr = null;
                if (xAttr != null)
                {
                    connStr = xAttr.Value;
                }
                try
                {
                    connBuilder = new SqlConnectionStringBuilder(connStr);
                }
                catch (Exception ex)
                {
                    SMS.WinForms.ErrorBox.Show("配置文件中的数据库连接字符串格式错误，将重新创建。", ex);
                }

                if (connBuilder != null)
                {
                    txt_DataSource.Text = connBuilder.DataSource;
                    cmb_InitialCatalog.Text = connBuilder.InitialCatalog;
                    cmb_ValidateType.SelectedIndex = connBuilder.IntegratedSecurity ? 0 : 1;
                    pnl_UserPassword.Enabled = !connBuilder.IntegratedSecurity;
                    if (pnl_UserPassword.Enabled == true)
                    {
                        txt_UserName.Text = connBuilder.UserID;
                        txt_Password.Text = connBuilder.Password;
                    }
                }

            }
        }

        private void cmb_ValidateType_SelectedIndexChanged(object sender, EventArgs e)
        {
            pnl_UserPassword.Enabled = cmb_ValidateType.SelectedIndex == 1;
        }

        public override bool CanNavigate(SMS.WinForms.WizardFormNavigationEventArgs e)
        {
            try
            {
                if (e.NavigationType == SMS.WinForms.WizardFormNavigationTypes.Next)
                {
                    string connStr = BuildConnString();
                    SetConnNodeValue(connStr);
                }
            }
            catch (Exception ex)
            {
                ErrorBox.Show(ex);
                return false;
            }
            return true;
        }

        private string BuildConnString()
        {
            if (connBuilder == null) connBuilder = new SqlConnectionStringBuilder();
            connBuilder.DataSource = txt_DataSource.Text;
            connBuilder.InitialCatalog = cmb_InitialCatalog.Text;
            bool integratedSecurity = cmb_ValidateType.SelectedIndex == 0;
            connBuilder.IntegratedSecurity = integratedSecurity;
            if (!integratedSecurity)
            {
                connBuilder.UserID = txt_UserName.Text;
                connBuilder.Password = txt_Password.Text;
            }
            string connStr = connBuilder.ToString();
            return connStr;
        }

        private void SetConnNodeValue(string connstr)
        {
            if (connNode == null)
            {
                XmlNode configurationNode = XmlHelper.LoadNode(this.Document, "configuration");
                XmlNode appSettingsNode = XmlHelper.LoadNode(configurationNode, "appSettings");
                connNode = XmlHelper.LoadNode(appSettingsNode, "add");
                XmlAttribute keyAttr = this.Document.CreateAttribute("key");
                keyAttr.Value = "connstr";
                connNode.Attributes.Append(keyAttr);
            }
            XmlAttribute xAttr = connNode.Attributes["value"];
            if (xAttr != null)
            {
                xAttr.Value = connstr;
            }
            else
            {
                XmlAttribute valueAttr = this.Document.CreateAttribute("value");
                valueAttr.Value = connstr;
                connNode.Attributes.Append(valueAttr);
            }
        }



        private void btn_Test_ConnectionStringRequest(object sender, ConnectionTestButton.ConnectionStringRequestArgs e)
        {
            e.ConnectionString = BuildConnString();
        }

        //private XmlNode LoadNode(XmlNode parentNode, string nodeName)
        //{
        //    XmlNode node = parentNode.SelectSingleNode(nodeName);
        //    if (node == null)
        //    {
        //        node = this.Document.CreateElement(nodeName);
        //        parentNode.AppendChild(node);
        //    }
        //    return node;
        //}
    }
}
